package com.demo.dbcompare.model.dto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.Assert;

import com.demo.dbcompare.model.CompareUtils;
import com.demo.dbcompare.model.TableColumnInfo;
import com.demo.dbcompare.model.vo.DbCompareDiffVo;

/**
 * 左表、右表、左表所有列信息、右表所有列信息的集合
 * @author: jie.deng
 * @time: 2018年1月10日 下午6:00:06
 */
public class TableRelationDto {
	
	public static final String SEPARATOR = "-";
	
	// 左连接名
	private String connNameLeft;
	// 左表名
	private String tableNameLeft;
	// 查询数据库得到的左表的列信息列表
	private List<TableColumnInfo> tableColumnInfoListLeft = new ArrayList<>();
	
	// 右连接名
	private String connNameRight;
	// 右表名
	private String tableNameRight;
	// 查询数据库得到的右表的列信息列表
	private List<TableColumnInfo> tableColumnInfoListRight = new ArrayList<>();
	
	public TableRelationDto(String connNameLeft, String tableNameLeft, String connNameRight, String tableNameRight) {
		super();
		this.connNameLeft = connNameLeft;
		this.tableNameLeft = tableNameLeft;
		this.connNameRight = connNameRight;
		this.tableNameRight = tableNameRight;
	}
	
	public List<DbCompareDiffVo> genColumnDiff() {
		List<DbCompareDiffVo> dbCompareDiffList = new ArrayList<>();
		
		if (CollectionUtils.isEmpty(tableColumnInfoListLeft) && CollectionUtils.isEmpty(tableColumnInfoListRight)) {
			return dbCompareDiffList;
		} else if (CollectionUtils.isNotEmpty(tableColumnInfoListLeft)
				&& CollectionUtils.isEmpty(tableColumnInfoListRight)) {
			// 左连接有表信息，右连接表缺失的数据，每张表产生一条差异记录
			dbCompareDiffList.addAll(CompareUtils.genDefaultTableDiff(connNameLeft, connNameRight, tableNameLeft, ""));
			return dbCompareDiffList;
		} else if (CollectionUtils.isEmpty(tableColumnInfoListLeft)
				&& CollectionUtils.isNotEmpty(tableColumnInfoListRight)) {
			// 右连接有表信息，左连接表缺失的数据，每张表产生一条差异记录
			dbCompareDiffList.addAll(CompareUtils.genDefaultTableDiff(connNameLeft, connNameRight, "", tableNameRight));
			return dbCompareDiffList;
		}
		
		//左右连接都有表信息，但是字段结构存在差异的，每个字段产生一条差异记录
		Map<String, TableColumnInfo> tableColumnInfoMapLeft = new TreeMap<>();
		for (TableColumnInfo tableColumnInfo : tableColumnInfoListLeft) {
			tableColumnInfoMapLeft.put(tableNameLeft+SEPARATOR+tableNameRight+SEPARATOR+tableColumnInfo.getColumnName(), tableColumnInfo);
		}
		Map<String, TableColumnInfo> tableColumnInfoMapRight = new TreeMap<>();
		for (TableColumnInfo tableColumnInfo : tableColumnInfoListRight) {
			tableColumnInfoMapRight.put(tableNameLeft+SEPARATOR+tableNameRight+SEPARATOR+tableColumnInfo.getColumnName(), tableColumnInfo);			
		}
		for (String key : getDistinctKeyList(tableColumnInfoMapLeft, tableColumnInfoMapRight)) {
			TableColumnInfo tableColumnInfoLeft = tableColumnInfoMapLeft.get(key);
			TableColumnInfo tableColumnInfoRight = tableColumnInfoMapRight.get(key);
			if (tableColumnInfoLeft == null && tableColumnInfoRight == null) {
				continue;
			}
			if (!Objects.equals(tableColumnInfoLeft, tableColumnInfoRight)) {
				String[] strs = key.split(SEPARATOR);
				Assert.isTrue(strs.length == 3);
				DbCompareDiffVo dbCompareDiff = new DbCompareDiffVo();
				dbCompareDiff.setConnNameLeft(connNameLeft);
				dbCompareDiff.setConnNameRight(connNameRight);
				dbCompareDiff.setTableNameLeft(strs[0]);
				dbCompareDiff.setTableNameRight(strs[1]);
				dbCompareDiff.setColumnName(strs[2]);
				List<TableColumnInfo> list = new ArrayList<>(2);
				list.add(tableColumnInfoLeft == null ? new TableColumnInfo() : tableColumnInfoLeft);
				list.add(tableColumnInfoRight == null ? new TableColumnInfo() : tableColumnInfoRight);
				dbCompareDiff.setCompareColumnList(list);
				dbCompareDiffList.add(dbCompareDiff);
			}
		}

		return dbCompareDiffList;
	}
	
	private List<String> getDistinctKeyList(Map<String, TableColumnInfo> tableColumnInfoMapLeft,
			Map<String, TableColumnInfo> tableColumnInfoMapRight) {
		List<String> distinctKeyList = new ArrayList<>();
		distinctKeyList.addAll(tableColumnInfoMapLeft.keySet());
		for (String key : tableColumnInfoMapRight.keySet()) {
			if (!distinctKeyList.contains(key)) {
				distinctKeyList.add(key);
			}
		}
		return distinctKeyList;
	}
	

	public List<DbCompareDiffVo> genColumnSimilarities() {
		List<DbCompareDiffVo> dbCompareSimilarities = new ArrayList<>();
		
		if (CollectionUtils.isEmpty(tableColumnInfoListLeft) || CollectionUtils.isEmpty(tableColumnInfoListRight)) {
			return dbCompareSimilarities;
		}
		
		//左右连接都有表信息，但是字段结构存在差异的，每个字段产生一条差异记录
		Map<String, TableColumnInfo> tableColumnInfoMapLeft = new TreeMap<>();
		for (TableColumnInfo tableColumnInfo : tableColumnInfoListLeft) {
			tableColumnInfoMapLeft.put(tableNameLeft+SEPARATOR+tableNameRight+SEPARATOR+tableColumnInfo.getColumnName(), tableColumnInfo);
		}
		Map<String, TableColumnInfo> tableColumnInfoMapRight = new TreeMap<>();
		for (TableColumnInfo tableColumnInfo : tableColumnInfoListRight) {
			tableColumnInfoMapRight.put(tableNameLeft+SEPARATOR+tableNameRight+SEPARATOR+tableColumnInfo.getColumnName(), tableColumnInfo);			
		}
		for (String key : CompareUtils.intersect(tableColumnInfoMapLeft.keySet(), tableColumnInfoMapRight.keySet())) {
			TableColumnInfo tableColumnInfoLeft = tableColumnInfoMapLeft.get(key);
			TableColumnInfo tableColumnInfoRight = tableColumnInfoMapRight.get(key);
			if (tableColumnInfoLeft != null && Objects.equals(tableColumnInfoLeft, tableColumnInfoRight)) {
				String[] strs = key.split(SEPARATOR);
				Assert.isTrue(strs.length == 3);
				DbCompareDiffVo dbCompareDiff = new DbCompareDiffVo();
				dbCompareDiff.setConnNameLeft(connNameLeft);
				dbCompareDiff.setConnNameRight(connNameRight);
				dbCompareDiff.setTableNameLeft(strs[0]);
				dbCompareDiff.setTableNameRight(strs[1]);
				dbCompareDiff.setColumnName(strs[2]);
				List<TableColumnInfo> list = new ArrayList<>(2);
				list.add(tableColumnInfoLeft);
				list.add(tableColumnInfoRight);
				dbCompareDiff.setCompareColumnList(list);
				dbCompareSimilarities.add(dbCompareDiff);
			}
		}

		return dbCompareSimilarities;
	}

	public String getTableNameLeft() {
		return tableNameLeft;
	}

	public void setTableNameLeft(String tableNameLeft) {
		this.tableNameLeft = tableNameLeft;
	}

	public String getTableNameRight() {
		return tableNameRight;
	}

	public void setTableNameRight(String tableNameRight) {
		this.tableNameRight = tableNameRight;
	}

	public List<TableColumnInfo> getTableColumnInfoLeft() {
		return tableColumnInfoListLeft;
	}

	public void setTableColumnInfoLeft(List<TableColumnInfo> tableColumnInfoLeft) {
		this.tableColumnInfoListLeft = tableColumnInfoLeft;
	}

	public List<TableColumnInfo> getTableColumnInfoRight() {
		return tableColumnInfoListRight;
	}

	public void setTableColumnInfoRight(List<TableColumnInfo> tableColumnInfoRight) {
		this.tableColumnInfoListRight = tableColumnInfoRight;
	}

}
